package cn.zhengzhou.springbootmybatis.demo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class AccountC2 {
    private String name;
    private double money;
    private Lock lock = new ReentrantLock();
    public AccountC2(String name, double money) {
        this.name = name;
        this.money = money;
    }

    public void drawMoney(double money) {
        String name = Thread.currentThread().getName();
        //上锁
        lock.lock();

        try {
            if (this.money >= money) {
                System.out.println(name + "取钱成功，取出" + money + "元");
                this.money -= money;
                System.out.println(name + "取钱成功，还剩余" + this.money + "元");
            } else {
                System.out.println(name + "余额不足");
            }
        } finally {
            lock.unlock();//解锁
        }


    }

}
